from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
import pymysql
import sys

import database.admin.setting as setting
import interface.student.course_table as course_table
class course_table_window(QMainWindow,course_table.Ui_Form):###点击用户身份弹出的登陆小弹窗
    def __init__(self,id):
        QMainWindow.__init__(self)
        course_table.Ui_Form.__init__(self)
        self.setupUi(self)
        self.id=id
        self.setWindowTitle("学生课表")
        self.table.setColumnCount(6)
        self.table.setRowCount(12)
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.table.horizontalHeader().setSectionResizeMode(0,QHeaderView.ResizeToContents)
        self.table.verticalHeader().setVisible(False)
        self.table.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.table.verticalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents)
        self.table.setHorizontalHeaderLabels(['节次/周次', '星期一', '星期二', '星期三', '星期四','星期五'])
        self.init_table()
    def init_table(self):
        jie=['第一节','第二节','第三节','第四节','第五节','第六节','第七节','第八节','第九节','第十节','第十一节','第十二节']
        for i in range(12):
            temp=QTableWidgetItem(jie[i])
            temp.setTextAlignment(Qt.AlignCenter)
            self.table.setItem(i, 0, temp)
            self.table.item(i,0).setBackground(QtGui.QColor(130,220,130,120))
        for i in range(0,12,2):
            for j in range(1,6):
                self.table.setSpan(i,j,2,1)
        self.connect = pymysql.connect(**setting.config)
        self.cursor = self.connect.cursor()
        sql_ = "select distinct  c.cname,cs.w1,cs.w2,cs.c1,cs.c2,cs.day ,c.tname from select_course_state s, course_schedule cs,course c where s.sid=%s and s.status=1 and c.id=s.cid and cs.cid=c.id"
        self.cursor.execute(sql_,self.id)
        week_={1:"第一周",2:"第二周",3:"第三周",4:"第四周",5:"第五周",6:"第六周",7:"第七周",8:"第八周",9:"第九周",10:"第十周",11:"第十一周",
               12:"第十二周",13:"第十三周",14:"第十四周",15:"第十五周",16:"第十六周",17:"第十七周",18:"第十八周"}
        #day={'1':"一","2":"二",'3':"三","4":"四",'5':"五"}
        re=self.cursor.fetchall()
        print(re)
        for i in range(len(re)):
            for j in range(len(re[i][5])):
                print(self.table.item(re[i][3]-1, int(re[i][5][j]))==None)
                temp = QTableWidgetItem(re[i][0] + "\n" + week_[re[i][1]] + '-' + week_[re[i][2]] + '\n' + re[i][-1] + '\n')
                temp.setTextAlignment(Qt.AlignCenter)
                self.table.setItem(re[i][3]-1, int(re[i][5][j]), temp)
                self.table.item(re[i][3]-1, int(re[i][5][j])).setBackground(QtGui.QColor(20, 150, 250,120))
                print(self.table.item(re[i][3] - 1, int(re[i][5][j])).text())
if __name__=='__main__':
    app=QApplication(sys.argv)
    md=course_table_window('2018300354')
    md.show()
    sys.exit(app.exec_())